package com.ruoyi.api.worker;



import com.ruoyi.system.utils.TokenUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.JiajiaWorker;
import com.ruoyi.system.domain.JiajiaWorkerBankCard;
import com.ruoyi.system.service.IJiajiaWorkerBankCardService;
import com.ruoyi.system.service.IJiajiaWorkerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/worker/bankCard")
@Api(tags = "社工银行卡接口")
public class ApiWorkerBankCardController {

    @Autowired
    private IJiajiaWorkerBankCardService bankCardService;

    @Autowired
    private IJiajiaWorkerService workerService;

    @ApiOperation(value = "添加银行卡")
    @PostMapping("/addBankCard")
    public AjaxResult addBankCard(@RequestParam @ApiParam(value = "银行卡号") String code,
                                  @RequestParam @ApiParam(value = "银行名称") String banrName
    ) {

        //1.查询社工是否有银行卡
        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        if (worker==null)return AjaxResult.notLogin();
        JiajiaWorkerBankCard workerBankCard=new JiajiaWorkerBankCard();
        workerBankCard.setWid(worker.getId());
        List<JiajiaWorkerBankCard> workerBankCardList = bankCardService.selectJiajiaWorkerBankCardList(workerBankCard);
        if (!workerBankCardList.isEmpty())return AjaxResult.error("只能添加一张银行卡");
        //2.新增
        workerBankCard.setWid(worker.getId());
        workerBankCard.setCode(code);
        workerBankCard.setBanrName(banrName);
        bankCardService.insertJiajiaWorkerBankCard(workerBankCard);
        return AjaxResult.success("添加成功");
    }

    @ApiOperation(value = "银行卡列表")
    @PostMapping("/bankCardList")
    public AjaxResult bankCardList() {

        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        if (worker==null)return AjaxResult.notLogin();

        JiajiaWorkerBankCard jiajiaWorkerBankCard = new JiajiaWorkerBankCard();
        jiajiaWorkerBankCard.setWid(worker.getId());
        List<JiajiaWorkerBankCard> jiajiaWorkerBankCardList = bankCardService.selectJiajiaWorkerBankCardList(jiajiaWorkerBankCard);

        return AjaxResult.success(jiajiaWorkerBankCardList);
    }

    @ApiOperation(value = "删除银行卡")
    @PostMapping("/delBank")
    public AjaxResult delBank(@RequestParam Long id) {

        JiajiaWorkerBankCard bankCard = bankCardService.selectJiajiaWorkerBankCardById(id);
        if (bankCard == null)return AjaxResult.refreshAgain();

        bankCardService.deleteJiajiaWorkerBankCardById(id);
        return AjaxResult.success("删除成功");
    }
}
